package com.bwie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.DiscountMapper;
import com.bwie.domain.Discount;
import com.bwie.service.DiscountService;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class DiscountServiceImpl extends ServiceImpl<DiscountMapper, Discount> implements DiscountService{

    @Autowired
    private DiscountMapper discountMapper;

    @Override
    public R listAll(PageInfoVo pageInfoVo) {
        Page<Discount> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        Page<Discount> discountPage = discountMapper.listAll(page,pageInfoVo);

        return R.OK(discountPage);
    }

    @Override
    public R addDicCount(Discount discount) {
        String random = RandomStringUtils.randomNumeric(3);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddss");
        String format = simpleDateFormat.format(date);
        System.out.println("format"+format);
        discount.setDiscountNameId("YHQ"+format+random);
        System.out.println(discount.getDiscountNameId());

        String discountName = discount.getDiscountName();
        if (discountName.length()>25){
            return R.ERROR(50,"优惠券名称最多输入为25个字符");
        }

        discountMapper.insert(discount);
        return R.OK();
    }
}
